import { Component, OnInit } from '@angular/core';
import { Data, Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzFormatEmitEvent, NzMessageService, NzModalService } from 'ng-zorro-antd';
import { TreeNode } from '@angular/router/src/utils/tree';

/**
 * 企业管理-列表
 */

@Component({
    templateUrl: './list.html',
})
export class EnterpriseListComponent implements OnInit {
    pages = {
        pageNo: "1",       // 当前页
        pageSize: "10",      // 每页大小
        pages: "",      // 每页大小
        keyLevel: "",    // 监测级别 Key重点,NonKey非重点
        cityId: "3206",     // 市编号 (写死)
        areaId: "",  // 区编号
        status: "",  // 状态 Normal正常,Disabled已禁用,ToBeRectified待整改
        name: ""   // 企业名称};
    };
    departmentList = [];
    departmentTypes = {};
    memo;
    departmentTypesList = [];
    selectedRowData: any = {};
    isVisible = true;
    isLoading = false;
    editTable = false;
    list = []
    areaList = [];
    cityValue?: string;

    isAllDisplayDataChecked = false;
    isOperating = false;
    isIndeterminate = false;
    listOfDisplayData: Data[] = [];
    listOfAllData: Data[] = [];
    mapOfCheckedId: { [key: string]: boolean } = {};
    numberOfChecked = 0;

    nzEvent(event: NzFormatEmitEvent): void {
        console.log(event);
    }

    ngOnInit() {
        this.loadList();
        this.onChangeCity(3206);
    }

    checkAll(value: boolean): void {
        this.listOfDisplayData.filter(item => !item.disabled).forEach(item => (this.mapOfCheckedId[item.id] = value));
        this.refreshStatus();
    }

    refreshStatus(): void {
        this.isAllDisplayDataChecked = this.listOfDisplayData
            .filter(item => !item.disabled)
            .every(item => this.mapOfCheckedId[item.id]);
        this.isIndeterminate =
            this.listOfDisplayData.filter(item => !item.disabled).some(item => this.mapOfCheckedId[item.id]) &&
            !this.isAllDisplayDataChecked;
        this.numberOfChecked = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id]).length;
    }

    changeCurrentPage(e) {
        this.pages.pageNo = e;
        this.loadList();
    }
    // 企業名稱搜索
    search() {
        this.loadList();
    }
    loadList() {
        this.apiService.post('采集器列表', this.pages).then((res: any) => {
            this.list = res.data['list'];
            this.pages.pageNo = res.data['pageNo'];
            this.pages.pages = res.data['pages'];
        })
        this.list = [{code:'1',name:'mc',jg:'1',xj:'1.1',ssjg:"abc",time:'2020-12',status : 'Normal'}];
    }
    selectThis(item) {

    }
    confirm(item) {
        this.apiService.post('禁用企业', {
            "id": item.id
        }).then((res: any) => {
            this.nzMessageService.success('已禁用该企业')
        })
    }
    // 更换城市
    onChangeCity(cityId) {
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
